#!/usr/bin/env python3

import nmap

# 端口常数
SSH = 22
HTTP = 80
MYSQL = 3306

# nmap 参数
IPADDR = '192.168.0.1/24'
PORTS = '22,80,3306'
ARGS = '-T4 --host-timeout 100 --max-rtt-timeout 300ms --min-hostgroup 256'

nm = nmap.PortScanner()
ret = nm.scan(IPADDR, PORTS, arguments=ARGS)
hosts = nm.all_hosts()

print('ssh')
ssh_hosts = [x for x in hosts if nm[x].tcp(SSH)['state'] == 'open']
print(ssh_hosts)

print('http')
http_hosts = [x for x in hosts if nm[x].tcp(HTTP)['state'] == 'open']
print(http_hosts)

print('mysql')
mysql_hosts = [x for x in hosts if nm[x].tcp(MYSQL)['state'] == 'open']
print(mysql_hosts)
